class CategoriesController < ApplicationController
before_filter :check_authorization
  # GET /categories
  # GET /categories.xml
  def index
    @categories = Category.find(:all)

    respond_to do |format|
      format.html # index.rhtml
      format.xml  { render :xml => @categories.to_xml }
    end
  end

  # GET /categories/1
  # GET /categories/1.xml
  def show
    @category = Category.find(params[:id])

    respond_to do |format|
      format.html # show.rhtml
      format.xml  { render :xml => @category.to_xml }
    end
  end

  # GET /categories/new
  def new
    @category = Category.new
    render :partial=>'categories/new_item'
  end

  # GET /categories/1;edit
  def edit
    @category = Category.find(params[:id])
    render :partial=>'categories/edit_item'
  end

  def create_from_msg
    @category = Category.new
    @category.name = params[:name]
    @category.project_id = params[:project_id]
    @category.label = params[:label]
    if @category.save
      if params[:label] == 'files'
         fid = params[:fid]
	 cur_file = Files.find(fid)
	 cur_file.category_id = @category.id
	 cur_file.save
	 i   = params[:i]
         @cats = Project.find(params[:project_id]).file_categories
         render :update do |page|
	   Files.find(fid).session_files.each_with_index do |file,i|
	    page.replace_html 'cat_show_' + file.id.to_s,:partial=>'categories/cat_show',:locals=>{:cats => @cats,:cur_cat=>file.category_id,:label=>'files',:fid=>file.id,:i=>i}
	   end
	 end
      elsif params[:label] == 'msg'
         @cats = Project.find(params[:project_id]).msg_categories
         render :update do |page|
	   page['cat'].disabled = false
	   page.replace_html 'cat_show',:partial=>'categories/cat_show',:locals=>{:cats => @cats,:cur_cat=>@category.id,:label=>'msg'}
	 end
      else
         @cats = Project.find(params[:project_id]).wb_categories
         render :update do |page|
	   page['cat'].disabled = false
	   page.replace_html 'cat_show',:partial=>'categories/cat_show',:locals=>{:cats => @cats,:cur_cat=>@category.id,:label=>'writeboards'}
	 end
      end
    end
  end

  def save_file_cat
    fid = params[:fid]
    cat = params[:cat]
    file = Files.find(fid)
    file.category_id = cat
    file.save
    render :update do |page|
    end
  end

  # POST /categories
  # POST /categories.xml
  def create_from_sidebar
    @category = Category.new(params[:category])
    respond_to do |format|
      if @category.save
        format.html { 
	  render :update do |page|
            if @category.project_id
	      page.replace_html 'cat_side_show',:partial=>'categories/sidebar_list',:locals=>{:project=>@category.project,:label=>params[:category][:label]}
            else
	      if params[:new_cat].to_i > 0
	        page.replace_html 'cat_side_show_' + params[:new_cat],:partial=>'categories/sidebar_list',:locals=>{:company=>@category.company,:label=>params[:category][:label],:new_cat=>params[:new_cat]}
	      else
	       page.replace_html 'cat_side_show',:partial=>'categories/sidebar_list',:locals=>{:company=>@category.company,:label=>params[:category][:label]}
	      end
            end
	    page.visual_effect :highlight,'cat_item_' + @category.id.to_s
	    if params[:new_cat].to_i > 0
	      page.show 'new_cat_' + params[:new_cat]
            else
	      page.show 'new_cat'
            end
	  end
	}
        format.xml  { head :created, :location => category_url(@category) }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @category.errors.to_xml }
      end
    end
  end

  # PUT /categories/1
  # PUT /categories/1.xml
  def update
    @category = Category.find(params[:category][:id])

    respond_to do |format|
      if @category.update_attributes(params[:category])
        flash[:notice] = 'Category was successfully updated.'
        format.html { 
	 render :update do |page|
	 page.replace_html 'cat_item_' + @category.id.to_s, :partial=>'categories/show_item',:locals=>{:cat=>@category,:project=>@category.project}
	 end
	}
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @category.errors.to_xml }
      end
    end
  end

  def cancel_edit
    @category = Category.find(params[:id])

	 render :update do |page|
	 page.replace_html 'cat_item_' + @category.id.to_s, :partial=>'categories/show_item',:locals=>{:cat=>@category}
	end
  end
  def cancel_new
	 render :update do |page|
	 if params[:project_id]
	   page.replace_html 'new_cat', :partial=>'categories/new_item_label',:locals=>{:project=>Project.find(params[:project_id])}
         else
	   new_cat = (params[:new_cat].to_i > 0 ? "_" + params[:new_cat].to_s : "")
	   page.replace_html 'new_cat' + new_cat, :partial=>'categories/new_item_label',:locals=>{:project=>nil,:company=>Company.find(params[:company_id]),:new_cat=>params[:new_cat]}
         end
	end 
  end

  # DELETE /categories/1
  # DELETE /categories/1.xml
  def destroy
    @category = Category.find(params[:id])
    @category.destroy
    respond_to do |format|
      format.html { 
        render :update do |page|
	 if @category.project
	 page.replace_html 'cat_side_show',:partial=>'categories/sidebar_list',:locals=>{:project=>@category.project,:label=>@category.label,:new_cat=>nil}
	 page.hide 'rename'
	 page.show 'new_cat'
	 else
	   new_cat = (params[:new_cat].to_i > 0 ? "_" + params[:new_cat].to_s : "")
	   page.replace_html 'cat_side_show' + new_cat,:partial=>'categories/sidebar_list',:locals=>{:company=>@category.company,:label=>@category.label,:new_cat=>params[:new_cat]}
	 page.hide 'rename' + new_cat
	 page.show 'new_cat' + new_cat
	 end
	end
      }
      format.xml  { head :ok }
    end
  end
end
